<?php

namespace app\api1\controller;

use app\common\model\GptToken;
use DOMDocument;
use GuzzleHttp\Client;
use HeadlessChromium\BrowserFactory;
use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\exception\ValidateException;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 机器人文件
 *
 * api-RobotFiles
 *
 * @Apidoc\Title("机器人文件")
 * @Apidoc\Group("api")
 */
class RobotFiles extends Auth
{
    /**
     * list
     * @Apidoc\Title("列表")
     */
    public function list(): \think\response\Json
    {
        $robot_ids = \app\common\model\Robot::where('member_id', $this->request->uid)->column('id');
        $robot_id = input('robot_id',0,'intval');
        if(in_array($robot_id, $robot_ids)){
            $where = [];
            $where[] = ['robot_id', '=', $robot_id];
            $where[] = ['is_confirm', '=', 1];
            if(input('name')){
                $where[] = ['original_name', 'like', '%'.input('name').'%'];
            }
            $res = \app\common\model\RobotFiles::where($where)->order('id' ,'desc')->paginate(input('page_size'));
            $res = $res->toArray();
            foreach ($res['data'] as $index => $item) {
                $res['data'][$index]['is_url'] = $item['is_url'] ? '网址' : '文件';
                $res['data'][$index]['is_train'] = $item['is_train'] ? '已入库' : '训练中';
            }

            return success('ok', $res);
        }else{
            return error('该机器人不属于你，非法请求！');
        }
    }





    /**
     * delete
     * @Apidoc\Title("删除")
     * @Apidoc\Method("post")
     * @return Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete()
    {
        $id = request()->post('id');
        $res = \app\common\model\RobotFiles::where('id', $id)->find();
        if (!$res) {
            return error('文件不存在');
        }
        $robot = \app\common\model\Robot::where('id', $res['robot_id'])->find();
        if($robot['member_id'] != $this->request->uid){
            return error('非法请求');
        }
        $res->delete();
        \app\common\model\Robot::where('id', $robot['id'])->update(['is_train' => 2]);
        return success();
    }



    /**
     * status
     * @Apidoc\Title("状态")
     * @Apidoc\Method("post")
     */
    public function status()
    {
        $id = request()->post('id');
        $res = \app\common\model\RobotFiles::where('id', $id)->where('member_id', $this->request->uid)->find();
        if (!$res) {
            return error('文件不存在');
        }
        $res->status = $res->status == 1 ? 0 : 1;
        $res->save();
        return success();
    }

    /**
     * parseUrl
     * @Apidoc\Method("post")
     * @Apidoc\Title("解析url")
     * @return Json
     */
    public function parseUrl(): Json
    {
        $url = request()->post('url');
        $domain = parse_url($url, PHP_URL_HOST); // 解析提供的 URL 获取域名
        $https = substr($url, 0, 5) == 'https' ? 'https://' : 'http://';
        $html = file_get_contents($url); // 获取 URL 的内容
        $doc = new DOMDocument();
        @$doc->loadHTML($html);


        $links = [];
        $anchorTags = $doc->getElementsByTagName('a');

        foreach ($anchorTags as $tag) {
            $href = $tag->getAttribute('href');
            $parsedHref = parse_url($href);

            // 检查链接是否为同域名下的链接
            if ($parsedHref &&
                (
                    (!isset($parsedHref['host']) && substr($href,0,1) == '/')  ||
                    (isset($parsedHref['host'])  && $parsedHref['host'] === $domain)
                )
            ) {
                if(!isset($parsedHref['host'])){
                    $href = $https .  $domain . $href ;
                }
                $links[] = $href;
            }
        }


        $res = [];
        if($links){
            array_unshift($links, rtrim($url, '/'));
            $links = array_unique($links);
            foreach ($links as $link){
                $res[] = $link;
            }
        }else{
            try {
                $client = new Client([
                    'timeout' => 300,
                    'verify' => false,
                ]);
                $client_response = $client->request('post', config('transfer.python_address').'/parseUrl', [
                    'json' => [
                        'url' => $url
                    ],
                ]);
                $html = $client_response->getBody()->getContents();
                $html = json_decode($html, true);

                @$doc->loadHTML($html);

                $links = [];
                $anchorTags = $doc->getElementsByTagName('a');


                foreach ($anchorTags as $tag) {
                    $href = $tag->getAttribute('href');
                    $parsedHref = parse_url($href);
                     //检查链接是否为同域名下的链接
                    if ( $parsedHref){
                        if(!isset($parsedHref['host'])){
                            if(substr($href,0,1) == '/'){
                                $newUrl = $https .  $domain . $href ;
                                $links[] = $newUrl;
                            }else{
                                $parts = explode('/', $url);
                                $lastPart = end($parts);
                                array_pop($parts);
                                $parts[] = $href;
                                $newUrl = implode('/', $parts);
                                $links[] = $newUrl;
                            }
                        }
                        else{
                            if($parsedHref['host'] != $domain){
                                $links[] = $href;
                            }
                        }
                    }
                }
                array_unshift($links, rtrim($url, '/'));
                $links = array_unique($links);
                foreach ($links as $link){
                    $res[] = $link;
                }
            } catch (\Exception $e) {
                return error($e->getMessage());
            }
        }


        return success('ok', $res);
    }

    /**
     * addUrls
     * @Apidoc\Title("批量添加url")
     */
    public function addUrls()
    {
        $urls = request()->post('urls');
        $robot_id = request()->post('robot_id');
        $robot = \app\common\model\Robot::where('id', $robot_id)->where('member_id', $this->request->uid)->find();
        if(!$robot) {
            return error('非法请求，机器人不属于你！');
        }

        foreach ($urls as $url) {
            $data = [
                'robot_id' => $robot_id,
                'name' => $url,
                'is_url' => 1,
                'original_name' => $url,
            ];
            $robot_file = \app\common\model\RobotFiles::create($data);
        }

        \app\common\model\Robot::where('id', $robot_id)->update(['is_train' => 2]);
        return success();
    }

    /**
     * upload
     * @Apidoc\Title("上传文件")
     */
    public function upload()
    {
        $robot_id = input('robot_id',0,'intval');
        $file = request()->file();
        try{
            $rules = [
                'file' => [
                    'fileExt' =>  config('memberfile.robot_file_extension'),
                    'fileSize' =>   config('memberfile.robot_file_size') * 1024
                ]
            ];
            validate($rules)->check($file);
        }catch (ValidateException $e) {
            return error($e->getMessage(),'', '', 500);
        }
        $file = $file['file'];
        $save_path = \think\facade\Filesystem::disk('public')->putFile('memberUpload/' . $robot_id  , $file);
        $save_path = '/storage/' . $save_path;
        $robotFile = \app\common\model\RobotFiles::create([
            'robot_id' => $robot_id,
//            'name' =>  app()->getRootPath() . 'public' . $save_path,
            'name' =>  rtrim(config('wechat.base_url'),'/') . $save_path,
            'is_url' => 0,
            'original_name' => $file->getOriginalName(),
            'is_confirm' => 0,
        ]);
        \app\common\model\Robot::where('id', $robot_id)->update(['is_train' => 2]);
        return success('上传成功', ['url'=>$save_path, 'id'=>$robotFile->id]);
    }

    /**
     * parseSiteMap
     * @Apidoc\Title("解析站点地图")
     */
    public function parseSiteMap()
    {
        //设置时间不限
        set_time_limit(0);
        $url = request()->post('url');
        $urls = $this->extractUrlsFromSitemap($url);
        return success('ok', $urls);
    }

    function extractUrlsFromSitemap($sitemapUrl) {

        $xmlString = file_get_contents($sitemapUrl);

        if ($xmlString === false) {
            return array();
        }

        $urls = array();
        $xml = simplexml_load_string($xmlString);

        foreach ($xml->url as $url) {
            $urls[] = (string) $url->loc;
            if(count($urls) > 100){
                break;
            }

        }

        return $urls;
    }

    /**
     * confirmFiles
     * @Apidoc\Title("确认文件")
     */
    public function confirmFiles()
    {
        $ids = input('ids', []);
        if(sizeof($ids) == 0){
            return error('请选择文件');
        }
        $robot_id = input('robot_id',0,'intval');
        if(!$robot_id){
            return error('请选择机器人');
        }
        $robot = \app\common\model\RobotFiles::where('robot_id', $robot_id)
            ->whereIn('id', $ids)
            ->update(['is_confirm' => 1]);
        return success();
    }
}
